package com.easyschool.model.user;


import java.sql.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.hibernate.annotations.Type;

import com.easyschool.model.school.Class;
import com.easyschool.webservices.adapter.WSBooleanAdapter;
import com.easyschool.webservices.adapter.WSDateAdapter;


@XmlRootElement
@Entity
@Table(name="user")
public class User {

	@Id
	@GeneratedValue
	@Column(name = "IDUSER")
	private Integer id;
	@Column(name = "NAME")
	private String name;
	@Column(name = "SURNAME")
	private String surname;
	@Column(name = "BIRTHDAY")
	private Date birthday;
	@Column(name = "CREATE_DATE")
	private Date createDate;
	@Column(name = "ID_CLASS")
	private Integer idClass;
	
	@ManyToOne(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name="ID_CLASS", insertable=false, nullable=false, updatable=false)
	private Class userClass;
	
	@Column(name="IS_PROFESSOR")
	@Type(type="yes_no")
	private Boolean isProfessor;
	
	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "user_class", 
		joinColumns = {@JoinColumn(name="IDUSER", nullable=false)}, 
		inverseJoinColumns={@JoinColumn(name="IDCLASS", nullable=false)})
	private Set<Class> classes = new HashSet<Class>(0);
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	
	@XmlJavaTypeAdapter(WSDateAdapter.class)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	@XmlJavaTypeAdapter(WSDateAdapter.class)
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	
	public Integer getIdClass() {
		return idClass;
	}
	public void setIdClass(Integer idClass) {
		this.idClass = idClass;
	}
	@XmlJavaTypeAdapter(WSBooleanAdapter.class)
	public Boolean getIsProfessor() {
		return isProfessor;
	}
	public void setIsProfessor(Boolean isProfessor) {
		this.isProfessor = isProfessor;
	}
	
	public Set<Class> getClasses() {
		return classes;
	}
	public void setClasses(Set<Class> classes) {
		this.classes = classes;
	}
	
	
	
	public Class getUserClass() {
		return userClass;
	}
	public void setUserClass(Class userClass) {
		this.userClass = userClass;
	}
	public User(){
	}
	
	public User(Integer pid, String pname, String psurname, Date pbirthday, Date createDate, Integer idClass, Boolean isProfessor){
		this.id = pid;
		this.name = pname;
		this.surname = psurname;
		this.birthday = pbirthday;
		this.createDate = createDate;
		this.idClass = idClass;
		this.isProfessor = isProfessor;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj != null && obj instanceof User && ((User)obj).getId().equals(this.getId())){
			return true;
		} else {
			return false;
		}
	}
	
	
	
}
